package com.yisoo.ubag.app.controller;

import java.util.Arrays;
import java.util.Map;

import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yisoo.ubag.app.entity.PayEntity;
import com.yisoo.ubag.app.service.PayService;
import com.yisoo.common.utils.PageUtils;
import com.yisoo.common.utils.R;



/**
 * 
 *
 * @author mmciel
 * @email 761998179@qq.com
 * @date 2021-04-16 23:19:13
 */
@RestController
@RequestMapping("app/pay")
public class PayController {
    @Autowired
    private PayService payService;

    //兑换优惠券
    @RequestMapping("/use/coupon/{uid}")
    public R useCoupon(@PathVariable("uid") String uid, @RequestParam("cid")Integer cid){
        return payService.useCoupon(uid,cid);
    }


    //获取用户当前余额
    @RequestMapping("/get/coupon/{uid}")
    public R getCoupon(@PathVariable("uid") String uid){
        return payService.getCoupon(uid);
    }

    //获取用户当前余额
    @RequestMapping("/get/balance/{uid}")
    public R getBalance(@PathVariable("uid") String uid){
        return payService.getBalance(uid);
    }

    //检查用户是否为新用户
    @RequestMapping("/check/new/{uid}/{key}")
    public R checkNew(@PathVariable("uid") String uid,@PathVariable("key") Integer key){
        boolean ans = payService.checkNewUser(uid);
        if (key == 1){//检查是否为新用户，如果是新用户则创建用户并为其初始化钱包
            PayEntity payEntity = payService.getPayByUidWithNoPayToCreatePay(uid);
            return R.ok().put("ans",1).put("data",payEntity);
        }else if(key == 2){//检查是否为新用户,直接返回结果
            if (ans){
                return R.ok().put("ans",1);
            }else{
                return R.ok().put("ans",0);
            }
        }
        return R.error("request key error");
    }
//    @RequestMapping("/show/{uid}")
//    public R show(@PathVariable("uid") String uid){
//        return payService.showPay(uid);
//    }
//    @RequestMapping("/init/{uid}")
//    public R init(@PathVariable("uid") String uid){
//        return payService.initPay(uid);
//    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("app:pay:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = payService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{appUserId}")
    //@RequiresPermissions("app:pay:info")
    public R info(@PathVariable("appUserId") String appUserId){
		PayEntity pay = payService.getById(appUserId);

        return R.ok().put("pay", pay);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("app:pay:save")
    public R save(@RequestBody PayEntity pay){
		payService.save(pay);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("app:pay:update")
    public R update(@RequestBody PayEntity pay){
		payService.updateById(pay);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("app:pay:delete")
    public R delete(@RequestBody String[] appUserIds){
		payService.removeByIds(Arrays.asList(appUserIds));

        return R.ok();
    }

}
